package top.defaults.colorpicker;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

import java.util.Locale;

/**
 * ColorObserver
 *
 * @author: jjy
 * @since 2021-04-16
 */
public class ColorPickerPopup {

    private Context context;
    private CommonDialog popupWindow;
    private int initialColor;
    private boolean enableBrightness;
    private boolean enableAlpha;
    private String okTitle;
    private String cancelTitle;
    private boolean showIndicator;
    private boolean showValue;
    private ButtonClickListener mButtonClickListener;

    private ColorPickerPopup(Builder builder, ButtonClickListener buttonClickListener) {

        this.mButtonClickListener = buttonClickListener;
        init(builder);
    }

    private ColorPickerPopup(Builder builder) {
        init(builder);
    }

    private void init(Builder builder) {
        this.context = builder.context;
        this.initialColor = builder.initialColor;
        this.enableBrightness = builder.enableBrightness;
        this.enableAlpha = builder.enableAlpha;
        this.okTitle = builder.okTitle;
        this.cancelTitle = builder.cancelTitle;
        this.showIndicator = builder.showIndicator;
        this.showValue = builder.showValue;
    }

    private Component initPopupView() {
        LayoutScatter layoutScatter = LayoutScatter.getInstance(context);
        Component layout = null;
        if (layoutScatter == null) {
            return layout;
        }
        layout = layoutScatter.parse(ResourceTable.Layout_top_defaults_view_color_picker_popup,
            null, true);
        popupWindow = new CommonDialog(context);
        return layout;
    }

    /**
     * 显示颜色框
     *
     * @param parent 父布局
     * @param observer 颜色监听类
     */
    public void show(Component parent, final ColorPickerObserver observer) {
        Component layout = initPopupView();
        popupWindow.setContentCustomComponent(layout);
        popupWindow.setAlignment(LayoutAlignment.CENTER);
        popupWindow.setSize(ComponentContainer.LayoutConfig.MATCH_CONTENT,
            ComponentContainer.LayoutConfig.MATCH_CONTENT);
        AlphaSliderView alphaSliderView = (AlphaSliderView) layout.findComponentById(ResourceTable.Id_alphaSliderView);
        BrightnessSliderView brightnessSliderView =
            (BrightnessSliderView) layout.findComponentById(ResourceTable.Id_brightnessSliderView);
        final Component colorIndicator = layout.findComponentById(ResourceTable.Id_colorIndicator);
        final Text colorHex = (Text) layout.findComponentById(ResourceTable.Id_colorHex);
        colorIndicator.setVisibility(showIndicator ? Component.VISIBLE : Component.HIDE);
        colorHex.setVisibility(showValue ? Component.VISIBLE : Component.HIDE);
        final ColorPickerView colorPickerView =
            (ColorPickerView) layout.findComponentById(ResourceTable.Id_colorPickerView);
        popupWindow.setAutoClosable(true);
        colorPickerView.setEnabledBrightness(enableBrightness);
        colorPickerView.setEnabledAlpha(enableAlpha);
        colorPickerView.setColorChangeListener(new ColorChangeListener() {
            @Override
            public void onColorChange(int color) {
                colorHex.setText(colorHex(color));
                ShapeElement element = new ShapeElement();
                int[] rgbColors = ColorConverUtils.colorToRgb(color);
                element.setRgbColor(new RgbColor(rgbColors[0], rgbColors[1], rgbColors[2], Color.alpha(color)));
                colorIndicator.setBackground(element);
            }
        });
        colorPickerView.setInitialColor(initialColor, alphaSliderView, brightnessSliderView);
        Text cancel = (Text) layout.findComponentById(ResourceTable.Id_cancel);
        cancel.setText(cancelTitle);
        cancel.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                popupWindow.destroy();
            }
        });
        Text ok = (Text) layout.findComponentById(ResourceTable.Id_ok);
        ok.setText(okTitle);
        ok.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                popupWindow.destroy();
                if (ColorPickerPopup.this.mButtonClickListener != null) {
                    ColorPickerPopup.this.mButtonClickListener.okClick(colorPickerView.getCurrentColor());
                }
            }
        });
        popupWindow.show();
    }

    /**
     * dismiss dialog
     */
    public void dismiss() {
        if (popupWindow != null) {
            popupWindow.destroy();
        }
    }

    /**
     * ButtonClickListener
     *
     * @author: jjy
     * @since 2021-04-16
     */
    public interface ButtonClickListener {
        /**
         * 确定按钮
         *
         * @param color 颜色值
         */
        void okClick(int color);

        /**
         * 取消按钮
         */
        void cancelClick();
    }

    /**
     * ColorObserver
     *
     * @author: jjy
     * @since 2021-04-16
     */
    public static class Builder {

        private Context context;
        private int initialColor = Color.MAGENTA.getValue();
        private boolean enableBrightness = true;
        private boolean enableAlpha = false;
        private String okTitle = "OK";
        private String cancelTitle = "Cancel";
        private boolean showIndicator = true;
        private boolean showValue = true;
        private ButtonClickListener mButtonClickListener;

        /**
         * 构造函数
         *
         * @param context context对象
         */
        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 设置按钮监听事件
         *
         * @param buttonClickListener 按钮监听事件
         * @return Builder对象
         */
        public Builder setButtonClickListener(ButtonClickListener buttonClickListener) {

            this.mButtonClickListener = buttonClickListener;

            return this;
        }

        /**
         * 初始化颜色纸
         *
         * @param color 颜色纸
         * @return Builder对象
         */
        public Builder initialColor(int color) {
            initialColor = color;
            return this;
        }

        /**
         * 设置是否高亮
         *
         * @param enable 是否高亮
         * @return Builder对象
         */
        public Builder enableBrightness(boolean enable) {
            enableBrightness = enable;
            return this;
        }


        /**
         * 设置是否支持设置透明度
         *
         * @param enable 是否透明
         * @return Builder对象
         */
        public Builder enableAlpha(boolean enable) {
            enableAlpha = enable;
            return this;
        }

        /**
         * ok按钮标题
         *
         * @param title ok按钮的标题
         * @return Builder对象
         */
        public Builder okTitle(String title) {
            okTitle = title;
            return this;
        }

        /**
         * 取消按钮标题
         *
         * @param title 取消按钮标题
         * @return Builder对象
         */
        public Builder cancelTitle(String title) {
            cancelTitle = title;
            return this;
        }

        /**
         * 是否显示着色板
         *
         * @param show 是否显示着色板
         * @return Builder对象
         */
        public Builder showIndicator(boolean show) {
            showIndicator = show;
            return this;
        }

        /**
         * 是否显示着色板
         *
         * @param show 是否显示着色板
         * @return Builder对象
         */
        public Builder showValue(boolean show) {
            showValue = show;
            return this;
        }

        /**
         * 是否显示着色板
         *
         * @param only 是否显示着色板
         * @return Builder对象
         */
        public Builder onlyUpdateOnTouchEventUp(boolean only) {
            return this;
        }

        /**
         * 构造ColorPickerPopup对象
         *
         * @return ColorPickerPopup
         */
        public ColorPickerPopup build() {
            if (mButtonClickListener == null) {
                return new ColorPickerPopup(this);
            }

            return new ColorPickerPopup(this, mButtonClickListener);
        }
    }

    private String colorHex(int color) {
        int aa = Color.alpha(color);
        int[] rgbColor = ColorConverUtils.colorToRgb(color);
        int rr = rgbColor[0];
        int gg = rgbColor[1];
        int bb = rgbColor[2];
        return String.format(Locale.getDefault(), "0x%02X%02X%02X%02X", aa, rr, gg, bb);
    }

    /**
     * ColorPickerObserver
     *
     * @author: jjy
     * @since 2021-04-16
     */
    public abstract static class ColorPickerObserver implements ColorObserver {
        /**
         * 选择颜色回调
         *
         * @param color 颜色纸
         */
        public abstract void onColorPicked(int color);

        @Override
        public final void onColor(int color, boolean fromUser, boolean shouldPropagate) {

        }
    }
}
